﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

/// <summary>
/// Summary description for MovieAccess
/// </summary>
/// 

public struct Movie{
    public int movieId;
    public string title;
    public string description;
    public string cast;
    public int godina;
    public string srcPath;
    public string srcPicture;
    public string director;
    public string country;
    public bool freeBit;
    public bool promoBit;
}
public static class MovieAccess
{


    static MovieAccess()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    //врати инфо за филм
    public static Movie GetMovieDetails(int movieId)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "GetMovieDetails";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@idMovie";
        parameter.Value =movieId;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        Movie movieDetails = new Movie();
        if (dataTable.Rows.Count > 0)
        {
            DataRow dr = dataTable.Rows[0];
            movieDetails.movieId = movieId;
            movieDetails.title = dr["title"].ToString();
            movieDetails.srcPicture = dr["srcPicture"].ToString();
            movieDetails.srcPath = dr["srcPath"].ToString();
            movieDetails.description = dr["description"].ToString();
            movieDetails.cast = dr["cast"].ToString();
            movieDetails.director = dr["director"].ToString();
            movieDetails.godina = Convert.ToInt32(dr["godina"].ToString());
            movieDetails.freeBit = Convert.ToBoolean(dr["freebit"]);
            movieDetails.promoBit = Convert.ToBoolean(dr["promobit"]);
        }
        return movieDetails;
    }

    // врати листа од филмови во жанр.
    public static DataTable GetMoviesInGenre(int genre, int pageNumber, out int howManyPages, int type)
    {
        DbCommand comm =  GenericDataAccess.CreateCommand();
        comm.CommandText = "GetMoviesInGenre";
        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@idGenre";
        parameter.Value = genre;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@PageNumber";
        parameter.Value = pageNumber;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@MoviesPerPage";
        parameter.Value = MovieConfig.MoviesPerPage;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@FilmType";
        parameter.Value = type;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@HowManyMovies";
        parameter.Direction=ParameterDirection.Output;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        int howManyMovies = Int32.Parse(comm.Parameters["@HowManyMovies"].Value.ToString());
        howManyPages = (int)Math.Ceiling((double)howManyMovies / (double)MovieConfig.MoviesPerPage);
        return dataTable;
    }
    
    // врати листа од филмови во жанр на HomePage.aspx
    public static DataTable GetMoviesInGenreHomePage(int genre, int type)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "GetMoviesInGenreHomePage";
        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@idGenre";
        parameter.Value = genre;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@FilmType";
        parameter.Value = type;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        
        return dataTable;
    }
    
    //врати листа од промо филмови
    public static DataTable GetPromoMovies()
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "GetPromoMovies";

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        return dataTable;
    }

    //врати листа од бесплатни филмови
    public static DataTable GetFreeMovies()
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "GetFreeMovies";

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        return dataTable;
    }

    //врати листа од најнови филмови
    public static DataTable GetNewMovies()
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "GetNewMovies";

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        return dataTable;
    }

    //врати листа од топ 10 филмови (по Favourites)
    public static DataTable GetTopMovies()
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "GetTopMovies";

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        return dataTable;
    }

    //врати листа од популарни 10 филмови (по Watchlist)
    public static DataTable GetPopularMovies()
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "GetPopularMovies";

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        return dataTable;
    }
    //стави User
    public static bool AddUser(string username, string mail)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "AddUser";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@username";
        parameter.Value = username;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@email";
        parameter.Value = mail;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);
        int result = -1;
        try
        {
            result = GenericDataAccess.ExecuteNonQuery(comm);
        }
        catch
        {
        }
        return (result != -1);
    }

    //врати листа од Favorites за даден корсник
    public static DataTable GetFavoriteMovies(string username)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "GetFavoriteMovies";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@username";
        parameter.Value = username;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        return dataTable;
    }

    //врати листа од Watchlist за даден корсник
    public static DataTable GetWatchMovies(string username)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "GetWatchMovies";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@username";
        parameter.Value = username;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);

        DataTable dataTable = GenericDataAccess.ExecuteSelectCommand(comm);
        return dataTable;
    }
    //додади филм во Favourites
    public static bool AddFavouriteMovie(int idMovie,string username)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "AddFavouriteMovie";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@idMovie";
        parameter.Value = idMovie;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@username";
        parameter.Value = username;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);
        int result = -1;
        try
        {
            result = GenericDataAccess.ExecuteNonQuery(comm);
        }
        catch
        {
        }
        return (result != -1);
    }

    //додади филм во Watchlist
    public static bool AddWatchlistMovie(int idMovie, string username)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "AddWatchlistMovie";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@idMovie";
        parameter.Value = idMovie;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@username";
        parameter.Value = username;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);
        int result = -1;
        try
        {
            result = GenericDataAccess.ExecuteNonQuery(comm);
        }
        catch
        {
        }
        return (result != -1);
    }

    //избриши филм во Watchlist
    public static bool RemoveWatchlistMovie(int idMovie, string username)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "RemoveWatchlistMovie";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@idMovie";
        parameter.Value = idMovie;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@username";
        parameter.Value = username;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);
        int result = -1;
        try
        {
            result = GenericDataAccess.ExecuteNonQuery(comm);
        }
        catch
        {
        }
        return (result != -1);
    }

    //избриши филм од Favourites
    public static bool RemoveFavouriteMovie(int idMovie, string username)
    {
        DbCommand comm = GenericDataAccess.CreateCommand();
        comm.CommandText = "RemoveFavouriteMovie";

        DbParameter parameter = comm.CreateParameter();
        parameter.ParameterName = "@idMovie";
        parameter.Value = idMovie;
        parameter.DbType = DbType.Int32;
        comm.Parameters.Add(parameter);

        parameter = comm.CreateParameter();
        parameter.ParameterName = "@username";
        parameter.Value = username;
        parameter.DbType = DbType.String;
        comm.Parameters.Add(parameter);
        int result = -1;
        try
        {
            result = GenericDataAccess.ExecuteNonQuery(comm);
        }
        catch
        {
        }
        return (result != -1);
    }

    
}